package test;

import proxy.User;
import proxy._static.ProxyRealSubject;
import proxy._static.RealSubject;
import proxy.Subject;
import proxy.dynamic.DynamicRealSubject;

import java.lang.reflect.Proxy;

public class ProxyMain {
    public static void main(String[] args) {
        User user = new User();
        ProxyRealSubject proxyRealSubject = new ProxyRealSubject(new RealSubject(user));
        System.out.println(proxyRealSubject.getUser());

        DynamicRealSubject dynamicRealSubject = new DynamicRealSubject(user);
        Subject subject = (Subject) Proxy.newProxyInstance(dynamicRealSubject.getClass().getClassLoader(), dynamicRealSubject.getClass().getInterfaces(), (proxy, method, arg) -> {
            System.out.println("代理方法之前执行");
            Object result = method.invoke(dynamicRealSubject, arg);
            System.out.println("代理方法之后执行");
            return result;
        });
        System.out.println(subject.getUser());
    }
}
